﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.SingleWell.Models;
using ePlanet.ResourceRequest.DataAccess;
using ePlanet.ResourceRequest.Services;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.Business;
using ePlanet.SingleWell.Services;
using Microsoft.Practices.Unity;
using Web.SystemConfig.Services;
using Wuqi.Webdiyer;
using System.Collections;
using DreamBuilder.Core.Container;

namespace Web.Views.ResourceRequestManage
{
    public partial class ResourceRequestManager : PageBase
    {
        [Dependency]
        public IResourceRequestService m_IResourceRequestService
        {
            get;
            set;
        }

        [Dependency]
        public IWellCatalogService m_IWellCatalogService
        {
            get;
            set;
        }

        private string m_WellType = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pagerDone.PageSizeChanging += new EventHandler(pagerDone_PageSizeChanging);
            this.pagerUndo.PageSizeChanging += new EventHandler(pagerUndo_PageSizeChanging);
            if (string.IsNullOrEmpty(Request.QueryString["wellType"]))
            {
                this.m_WellType = Request.QueryString["wellType"];
            }
            else
            {
                ePlanet.SingleWell.Models.WellCatalog wellCatalog = this.m_IWellCatalogService.GetWellCatalogByID(Request.QueryString["wellType"]);
                if (wellCatalog != null && wellCatalog.WELLCATALOG != null && wellCatalog.WELLCATALOG.Rows.Count != 0)
                {
                    this.m_WellType = this.m_IWellCatalogService.GetWellCatalogByID(Request.QueryString["wellType"]).WELLCATALOG[0].WELLCATALOGID;
                }
            }

            if (!IsPostBack)
            {
                this.BindData();
                this.BindDone();
            }
        }

        void pagerUndo_PageSizeChanging(object sender, EventArgs e)
        {
            this.gv_ResourceRequestList.PageSize = this.pagerUndo.PageSize;
            this.pagerUndo.CurrentPageIndex = 1;
            this.gv_ResourceRequestList.PageIndex = 1;
            this.BindData();
        }

        void pagerDone_PageSizeChanging(object sender, EventArgs e)
        {
            this.gv_ResourceRequestListDone.PageSize = pagerDone.PageSize;
            this.pagerDone.CurrentPageIndex = 1;
            this.gv_ResourceRequestListDone.PageIndex = 1;
            this.BindDone();
        }


        private void BindData()
        {
            int sum = 0;
            //DataTable oDataTable = this.m_IResourceRequestService.GetRequestListByWellDataID(ResourceRequestEnum.NewRequest, this.m_WellType);
            DataTable oDataTable = this.m_IResourceRequestService.QueryAgent(ResourceRequestEnum.NewRequest, pagerUndo.CurrentPageIndex, pagerUndo.PageSize, out sum);
            this.gv_ResourceRequestList.DataSource = oDataTable;
            this.gv_ResourceRequestList.DataBind();
            pagerUndo.RecordCount = sum;
        }

        private void BindDone()
        {
            int sum = 0;
            //DataTable oDataTable = this.m_IResourceRequestService.GetDone(null,this.m_WellType);
            DataTable oDataTable = this.m_IResourceRequestService.QueryDone(pagerDone.CurrentPageIndex, pagerDone.PageSize, out sum);
            this.gv_ResourceRequestListDone.DataSource = oDataTable;
            this.gv_ResourceRequestListDone.DataBind();
            pagerDone.RecordCount = sum;
        }

        protected void pagerUndo_PageChanging(object sender, PageChangingEventArgs e)
        {
            pagerUndo.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }

        protected void pagerDone_PageChanging(object sender, PageChangingEventArgs e)
        {
            pagerDone.CurrentPageIndex = e.NewPageIndex;
            BindDone();
        }

        protected void gv_ResourceRequestList_Bound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                try
                {
                    string attachmentID = string.Empty;
                    if (row["candownload"].ToString() == "1" || row["canview"].ToString() == "1")
                    {
                        attachmentID = this.GetAttachmentID(row["DataID"].ToString());
                    }

                    if (row["candownload"].ToString() == "1")
                    {
                        ((HyperLink)e.Row.Cells[5].Controls[0]).Text = "是";
                        if (string.IsNullOrEmpty(attachmentID))
                        {
                            ((HyperLink)e.Row.Cells[5].Controls[0]).Enabled = false;
                        }
                        else
                        {
                            ((HyperLink)e.Row.Cells[5].Controls[0]).NavigateUrl = string.Format("../SingleWellManage/SingleWellAttachDownload.aspx?AttachmentID={0}&DBHelperString=SingleWell", attachmentID);
                        }
                    }
                    else
                    {
                        e.Row.Cells[5].Text = "否";
                    }

                    if (row["canview"].ToString() == "1")
                    {
                        ((HyperLink)e.Row.Cells[6].Controls[0]).Text = "是";
                        if (string.IsNullOrEmpty(attachmentID))
                        {
                            ((HyperLink)e.Row.Cells[6].Controls[0]).Enabled = false;
                        }
                        else
                        {
                            ((HyperLink)e.Row.Cells[6].Controls[0]).NavigateUrl = string.Format("../../WebDoc/WebDoc.aspx?AttachmentID={0}&AttachmentDBHelperString=SingleWell", attachmentID);
                        }
                    }
                    else
                    {
                        e.Row.Cells[6].Text = "否";
                    }
                }
                catch (Exception)
                {

                }
            }
        }

        private string GetAttachmentID(string p)
        {
            try
            {
                IAttachmentService oIAttachmentService = UContainer.Resolve<IAttachmentService>();
                oIAttachmentService.Initialize("SingleWell");
                List<AttachmentModel> modelList = oIAttachmentService.GetAttachmentModelByObjectID(p);
                if (modelList.Count > 0)
                {
                    return modelList[0].AttachmentID;
                }

                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        protected void btn_AgreeList_Click(object sender, EventArgs e)
        {
            ArrayList idList = this.gv_ResourceRequestList.SelectItems;
            if (idList.Count > 0)
            {
                List<Guid> dataidList = new List<Guid>();
                foreach (object obj in idList)
                {
                    dataidList.Add(new Guid(obj.ToString()));
                }

                this.m_IResourceRequestService.AgreeRequest(dataidList);
                this.BindData();
                Alert("批量审核成功");
            }
            else
            {
                Alert("请选择数据后再执行此操作!");
            }
        }

        protected void btn_DisAgreeList_Click(object sender, EventArgs e)
        {
            ArrayList idList = this.gv_ResourceRequestList.SelectItems;
            if (idList.Count > 0)
            {
                List<Guid> dataidList = new List<Guid>();
                foreach (object obj in idList)
                {
                    dataidList.Add(new Guid(obj.ToString()));
                }

                this.m_IResourceRequestService.DisAgreeRequest(dataidList);
                this.BindData();
                Alert("批量审核成功");
            }
            else
            {
                Alert("请选择数据后再执行此操作!");
            }
        }
    }
}
